﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;
using System.Globalization;

namespace blackhouse.BlackApart.Business.Furnitures {
	public class DaFurniture : IFurniture {

		#region Fields

		private IFurnitureDataAccess dataAccess;
		private ImpFurniture instance;

		#endregion

		#region Properties

		public int Id {
			get { return this.instance.Id; }
			private set { this.instance.Id = value; }
		}

		public string Name {
			get { return this.instance.Name; }
			set { this.instance.Name = value; }
		}

		public string Description {
			get { return this.instance.Description; }
			set { this.instance.Description = value; }
		}

		public string ImageName {
			get { return this.instance.ImageName; }
			set { this.instance.ImageName = value; }
		}

		public int Sort {
			get { return this.instance.Sort; }
			set { this.instance.Sort = value; }
		}

		#endregion

		#region Constructors

		public DaFurniture(IFurnitureDataAccess data)
			: this(data, 0) { }

		public DaFurniture(IFurnitureDataAccess data, int furnitureId) {
			this.dataAccess = data;
			if (furnitureId > 0)
				this.instance = this.ReadFurniture(furnitureId);
			if (this.instance == null)
				this.instance = new ImpFurniture();
		}

		#endregion

		#region Methods

		private ImpFurniture ReadFurniture(int furnitureId) {
			FurnitureData data = this.dataAccess.ReadFurniture(furnitureId, CultureInfo.CurrentCulture.Name);
			if (data.Id != furnitureId) return null;
			return this.Convert(data);
		}

		public ImpFurniture Convert(FurnitureData data) {
			ImpFurniture furniture = new ImpFurniture();
			furniture.Description = data.Description;
			furniture.Id = data.Id;
			furniture.ImageName = data.ImageName;
			furniture.Name = data.Name;
			furniture.Sort = data.Sort;
			return furniture;
		}

        public bool SaveFurniture() {
            if (this.instance.SaveFurniture()) {
				int id = this.dataAccess.SaveFurniture(this.Id, this.Name, this.Description, this.Sort, this.ImageName, this.instance.ImageData);
				if (id > 0) {
					this.Id = id;
					return true;
				}
				else return false;
			}
			else return false;
		}

		public void SetImageData(byte[] data) {
			this.instance.ImageData = data;
		}

		public bool AddToApartment(int apartmentId) {
			if (this.instance.AddToApartment(apartmentId))
				return this.dataAccess.AddFurnitureToApartment(this.Id, apartmentId);
			else return false;
		}

		public IFurniture[] GetApartmentFurnitures(int apartmentId) {
			FurnitureData[] data = this.dataAccess.GetApartmentFurnitures(apartmentId, CultureInfo.CurrentCulture.Name);
			return Array.ConvertAll<FurnitureData, IFurniture>(data, f => this.Convert(f));
		}

		public IFurniture[] GetApartmentTypeFurnitures(int apartmentTypeId) {
			FurnitureData[] data = this.dataAccess.GetApartmentTypeFurnitures(apartmentTypeId, CultureInfo.CurrentCulture.Name);
			return Array.ConvertAll<FurnitureData, IFurniture>(data, f => this.Convert(f));
		}

        public string GetFurnitureIconImageUrl(System.Drawing.Size size) {
            throw new NotImplementedException();
        }

		#endregion

    }
}
